<template>
    <div class="qingwu">
        <div class="admin_table_page_title">
            <a-button @click="$router.back()" class="float_right" icon="arrow-left">返回</a-button>
            平台用户编辑
        </div>
        <div class="unline underm"></div>
        <div class="admin_form">
            <a-form-model :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }">
                <a-form-model-item label="用户名">
                    <a-input v-model="info.username"></a-input>
                </a-form-model-item>

                <a-form-model-item label="手机号码">
                    <a-input v-model="info.phone"></a-input>
                </a-form-model-item>


                <a-form-model-item label="密码">
                    <a-input type="password" v-model="info.password" placeholder=""></a-input>
                </a-form-model-item>

                <a-form-model-item label="昵称">
                    <a-input v-model="info.nickname"></a-input>
                </a-form-model-item>

                <a-form-model-item label="积分">
                    <a-input v-model="info.integral"></a-input>
                </a-form-model-item>

                <a-form-item label="所属小区">
                    <a-select  @change="handleChange" :default-value="default_community_id" >
                        <a-select-option  :value="value.community_id" v-for="(value,index) in community_lists" :key="index">
                            {{ value.community_name }}
                        </a-select-option>
                    </a-select>
                </a-form-item>

                <a-form-model-item :wrapper-col="{ span: 12, offset: 5 }">
                    <a-button type="primary" @click="handleSubmit">提交</a-button>
                </a-form-model-item>
            </a-form-model>

        </div>
    </div>
</template>

<script>
export default {
    components: {},
    props: {},
    data() {
      return {
          info:{
          },
          id:0,
          community_lists:[],    // 小区列表
          default_community_id:'请选择小区'   // 默认
      };
    },
    watch: {},
    computed: {},
    methods: {
        handleSubmit(){
            // 验证代码处
            if(this.$isEmpty(this.info.username)){
                return this.$message.error('用户名不能为空');
            }
            if(this.$isEmpty(this.info.phone)){
                return this.$message.error('手机不能为空');
            }

            if(this.$isEmpty(this.info.community_id)){
                return this.$message.error('小区不能为空');
            }

            let api = this.$apiHandle(this.$api.adminUsers,this.id);
            if(api.status){
                this.$put(api.url,this.info).then(res=>{
                    if(res.code == 200){
                        this.$message.success(res.msg)
                        return this.$router.back();
                    }else{
                        return this.$message.error(res.msg)
                    }
                })
            }else{
                this.$post(api.url,this.info).then(res=>{
                    if(res.code == 200){
                        this.$message.success(res.msg)
                        return this.$router.back();
                    }else{
                        return this.$message.error(res.msg)
                    }
                })
            }


        },
        get_info(){
            this.$get(this.$api.adminUsers+'/'+this.id).then(res=>{
                res.data.password = undefined;
                this.info = res.data;
                this.default_community_id = res.data.community_id;
            })
        },
        // 获取列表
        onload(){
            // 判断你是否是编辑
            if(!this.$isEmpty(this.$route.params.id)){
                this.id = this.$route.params.id;
                this.get_info();
            }
        },
        // 获取小区列表
        getCommunityList(){
            this.$get(this.$api.adminCommunity).then(res=>{
                this.community_lists = res.data.data;
            })
        },
        // 小区选择
        handleChange(value){
            this.info.community_id = value;
        }


    },
    created() {
        this.onload();
        this.getCommunityList();
    },
    mounted() {}
};
</script>
<style lang="scss" scoped>

</style>
